package com.magicborrow.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.adapter.BorrowingAdapter;
import com.magicborrow.adapter.WantBorrowingAdapter;
import com.magicborrow.beans.StuffListBean;
import com.magicborrow.beans.WantBorringListBean;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.RefreshRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;

public class WantBorrowingFragment extends Fragment implements VolleyTool.HTTPListener {
    private RefreshRecyclerView rc;
    private WantBorrowingAdapter adapter;
    private ArrayList<WantBorringListBean.DataEntity.WantBorrowingBean> data;
    private int currentPage;
    private int PageSize = 20;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_item_list, container, false);
        rc = (RefreshRecyclerView)v.findViewById(R.id.list);

        data = new ArrayList<>();
        adapter = new WantBorrowingAdapter(data);
        rc.setAdapter(adapter);
        rc.setLoadFinish();
        rc.setOnRefreshListener(new RefreshRecyclerView.OnRefreshListener() {
            @Override
            public void onRefreshListener() {
                currentPage = 0;
                initData();

            }
        });
        rc.setOnPullListener(new RefreshRecyclerView.onPullListener() {
            @Override
            public void onPullListener() {
                currentPage++;
                initData();

            }
        });
        initData();
        v.findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rc.getManager().smoothScrollToPosition(rc.getRv(), null, 0);
            }
        });
        return v;
    }

    private void initData() {
        HashMap<String, String> params = new HashMap<>();
        params.put("page", "" + currentPage);
        params.put("size", "" + PageSize);
        VolleyTool.get(Constants.URL_GET_WANGT_BORROWING_LIST, params, this, 0, WantBorringListBean.class);

    }


    @Override
    public <T> void onResponse(T t, int requestCode) {
        WantBorringListBean b = (WantBorringListBean) t;
        if (b.getCode() >= 0) {
            if (currentPage == 0) {
                data.clear();
            }
            data.addAll(b.getData().getContent());
            rc.setIsLastPage(b.getData().isLast());
            adapter.notifyDataSetChanged();

        } else {
            Toast.makeText(getActivity(), b.getMessage(), Toast.LENGTH_SHORT).show();
            if (currentPage > 0) {
                currentPage--;
            }
        }
        rc.setLoadFinish();

    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        if (currentPage > 0) {
            currentPage--;
        }
        rc.setLoadFinish();

    }


}
